package com.auicyh.tylb.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

/**
 * 作者：  Ocean 2017/6/9 15:08
 * 功能：  Aui工具类
 * Q Q：
 * 修改：
 * @author Ocean
 */

public class NetworkUtils {
    /** 没有网络 */
    public static int NETWORK_NOT = 0;
    /** WIFI */
    public static int NETWORK_WIFI = 1;
    /** 4G */
    public static int NETWORK_4G = 4;
    /** 3G */
    public static int NETWORK_3G = 3;
    /** 2G */
    public static int NETWORK_2G = 2;

    /**
     * 获取当前的网络状态 ：没有网络-0：WIFI网络1：4G网络-4：3G网络-3：2G网络-2
     * 自定义
     * @param context 上下问
     * @return 网络信息
     */
    public static int getAPNType(Context context) {
        // 结果返回值
        int netType = NETWORK_NOT;
        // 获取手机所有连接管理对象
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        // 获取NetworkInfo对象
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        // NetworkInfo对象为空 则代表没有网络
        if (networkInfo == null) {
            return netType;
        }
        // 否则 NetworkInfo对象不为空 则获取该networkInfo的类型
        int nType = networkInfo.getType();
        if (nType == ConnectivityManager.TYPE_WIFI) {
            // WIFI
            netType = NETWORK_WIFI;
        } else if (nType == ConnectivityManager.TYPE_MOBILE) {
            int nSubType = networkInfo.getSubtype();
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            if (nSubType == TelephonyManager.NETWORK_TYPE_LTE && !telephonyManager.isNetworkRoaming()) {
                netType = NETWORK_4G;
            } else if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS
                    || nSubType == TelephonyManager.NETWORK_TYPE_HSDPA
                    || nSubType == TelephonyManager.NETWORK_TYPE_EVDO_0
                    && !telephonyManager.isNetworkRoaming()) {
                // 3G   联通的3G为UMTS或HSDPA 电信的3G为EVDO
                netType = NETWORK_3G;
                //2 G 移动和联通的2G为GPRS或EGDE，电信的2G为CDMA
            } else if (nSubType == TelephonyManager.NETWORK_TYPE_GPRS
                    || nSubType == TelephonyManager.NETWORK_TYPE_EDGE
                    || nSubType == TelephonyManager.NETWORK_TYPE_CDMA
                    && !telephonyManager.isNetworkRoaming()) {
                netType = NETWORK_2G;
            } else {
                netType = NETWORK_2G;
            }
        }
        return netType;
    }

    /**
     * 判断是否有网络连接
     * @param context context
     * @return 是否有网络链接
     */
    public static boolean isNetworkConnected(Context context) {
        if (context != null) {
            // 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)
            ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            // 获取NetworkInfo对象
            if (manager != null) {
                NetworkInfo networkInfo = manager.getActiveNetworkInfo();
                //判断NetworkInfo对象是否为空
                if (networkInfo != null) {
                    int i = networkInfo.getType();
                    if (i == ConnectivityManager.TYPE_WIFI || i == ConnectivityManager.TYPE_MOBILE) {
                        return networkInfo.isConnectedOrConnecting();
                    }
                }
            }
        }
        return false;
    }

    /**
     * 判断WIFI是否可用
     * @param context context
     * @return true 可以用
     */
    public static boolean isWifiConnected(Context context) {
        if (context != null) {
            // 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)
            ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (manager != null) {
                // 获取NetworkInfo对象
                NetworkInfo networkInfo = manager.getActiveNetworkInfo();
                //判断NetworkInfo对象是否为空 并且类型是否为WIFI
          /*  if (networkInfo != null && networkInfo.getType() != ConnectivityManager.TYPE_WIFI)
                return networkInfo.isAvailable();*/
                if (networkInfo != null) {
                    int i = networkInfo.getType();
                    if (i == ConnectivityManager.TYPE_WIFI) {
                        return networkInfo.isConnectedOrConnecting();
                    }
                }
            }
        }
        return false;
    }

    /**
     * 判断移动网络是否可用
     * @param context context
     * @return true
     */
    public static boolean isMobileConnected(Context context) {
        if (context != null) {
            //获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)
            ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (manager != null) {
                //获取NetworkInfo对象
                NetworkInfo networkInfo = manager.getActiveNetworkInfo();
                //判断NetworkInfo对象是否为空 并且类型是否为MOBILE
                if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                    return networkInfo.isAvailable();
                }
            }
        }
        return false;
    }

    /**
     * 获取当前网络连接的类型信息
     * @param context context
     * @return -1
     */
    public static int getConnectedType(Context context) {
        if (context != null) {
            // 获取手机所有连接管理对象
            ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (manager != null) {
                //获取NetworkInfo对象
                NetworkInfo networkInfo = manager.getActiveNetworkInfo();
                if (networkInfo != null && networkInfo.isAvailable()) {
                    // 返回NetworkInfo的类型
                    return networkInfo.getType();
                }
            }

        }
        return -1;
    }

    /**
     * 判断网络是否可用
     * @param context context
     * @return 网络是否可用
     */
    public static boolean isNetWork(Context context) {
      /*  boolean b = false;
        int type = getAPNType(context);
        if (type == NetworkUtils.NETWORK_NOT) {
            b = false;
        } else if (type == NetworkUtils.NETWORK_2G || type == NetworkUtils.NETWORK_3G || type == NetworkUtils.NETWORK_4G) {
            b = isMobileConnected(context);
        } else if (type == NetworkUtils.NETWORK_WIFI) {
            b = isWifiConnected(context);
        }*/
        return isNetworkConnected(context);
    }

    /*
     * 判断GPS是否打开
     * ACCESS_FINE_LOCATION权限
     * @param context
     * @return
     */
 /*   public static boolean isGPSEnabled(Context context) {
        //获取手机所有连接LOCATION_SERVICE对象
        LocationManager locationManager = ((LocationManager) context.getSystemService(Context.LOCATION_SERVICE));
        return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }*/

}
